<?php

class Xangdau_model extends CI_Model
{
	// Protected or private properties
	protected $_table;
	
	// Constructor
	public function __construct()
	{
		parent::__construct();
		$this->load->helper('date');
		date_default_timezone_set('Asia/Ho_Chi_Minh');
	}
	
	
	// Lây danh sách do du dau ky khởi tạo
	function get_all_sddk()
	{
		$this->db->select('o_tong_hop.*, o_hang_muc.name as item, dvt.name as dvt');
		$this->db->from('o_tong_hop');
		$this->db->join('o_hang_muc', 'o_hang_muc.id = o_tong_hop.hang_muc_id', 'left');
		$this->db->join('dvt', 'dvt.id = o_hang_muc.dvt_id', 'left');
		$this->db->where('kind', 1);
		$query = $this->db->get();
		return $query->result_array();
	}
	
	// Public methods
	public function get_all_chung_tu()
	{
		$this->db->select('o_chung_tu.*, o_hang_muc.name, dvt.name as dvt, o_thiet_bi.code as tbname');
		$this->db->from('o_chung_tu');
		$this->db->join('o_hang_muc', 'o_hang_muc.id = o_chung_tu.hang_muc_id', 'left');
		$this->db->join('o_thiet_bi', 'o_thiet_bi.id = o_chung_tu.thiet_bi_id', 'left');
		$this->db->join('dvt', 'dvt.id = o_hang_muc.dvt_id', 'left');
		$this->db->where('o_chung_tu.created_date < NOW()');
		$this->db->where('o_chung_tu.created_date > DATE_ADD(NOW(),INTERVAL-3 MONTH)');
		$this->db->order_by('o_chung_tu.id','desc');
		$query = $this->db->get();
		return $query->result_array();
	}
	
	//////////////////////// END RP TONG HOP ///////////////
	function get_report_tong_hop($duration=null)
	{
		if($duration == null) $duration = 'THISMONTH';
		switch ($duration)
		{
			case 'THISMONTH':
				return $this->get_report_tong_hop_thang(); break;
			case 'THISYEAR':
				return $this->get_report_tong_hop_nam(); break;
			case 'QUATER':
				return $this->get_report_tong_hop_quy(); break;
			case 'HALF':
				return $this->get_report_tong_hop_nua(); break;
			default :
				return $this->get_report_tong_hop(); break;
		}
	
	}
	
	function get_report_tong_hop_thang()
	{
		$this->db->select('o_tong_hop.*, o_hang_muc.name as item, o_thiet_bi.name as tbname');
		$this->db->from('o_tong_hop');
		$this->db->join('o_hang_muc', 'o_hang_muc.id = o_tong_hop.hang_muc_id', 'left');
		$this->db->join('o_thiet_bi', 'o_thiet_bi.id = o_tong_hop.thiet_bi_id', 'left');
		$this->db->order_by('o_tong_hop.id','desc');
		$this->db->where('o_tong_hop.kind', 0);
		$this->db->where('YEAR(o_tong_hop.created_date)',date('Y'));
		$this->db->where('MONTH(o_tong_hop.created_date)',date('m'));
		$query = $this->db->get();
	
		$rss = $query->result_array();
		$ret = array();
		foreach($rss as $rs)
		{
			$sum = $this->tinh_tong_ps_theo_hang_muc_thang($rs['hang_muc_id']);
			if($sum)
			{
				if($sum[0]['tang'] == null) $sum[0]['tang'] = 0;
				if($sum[0]['giam'] == null) $sum[0]['giam'] = 0;
				$rs['ps_tang'] = $sum[0]['tang'];
				$rs['ps_giam'] = $sum[0]['giam'];
			}
			$ret[] = $rs;
		}
		return $ret;
	}
	function get_report_tong_hop_nam()
	{
		return null;
	}
	function get_report_tong_hop_quy()
	{
		return null;
	}
	function get_report_tong_hop_nua()
	{
		return null;
	}
	
	public function count_all_chung_tu()
	{
		$this->db->select('id');
		$this->db->from('f_chung_tu');
		return $this->db->count_all_results();
	}
	
	public function change_status_article($id = null,$status = 0)
	{
		$data = array
				(
					'status'=> $status
				);
		$this->db->where('id',$id);
		$this->db->update('site_content',$data);		
	}
	
}

/* End of file posts_model.php */
/* Location: ./application/modules/admin/models/posts_model.php */